require(['loading'], function(loading){
	//应用实例化
	var buyList = new Vue({
		el: '#buyList',
		data: {
			buyList: [],
			provinceData:[],
			oneClassData: [],
			twoClassData:[],
			showLoading: false,
			noData: false,
			apiUrl: wxUrl + '/v3.3/tbuyingInfo/list',
			areaUrl: wxUrl + '/v3.3/tbuyingInfo/areaList',
			classUrl: wxUrl + '/v3.3/tbuyingInfo/findSonGoodsClass',
			item: {
				params: {
					token: token,
					userToken: userToken,
					areaId: '',
					categoryId: '',
					pageNum: 1,
					pageSize: 20
				}
			},
			areaItem: {
				params: {
					token: token,
					userToken: userToken,
					parentId: ''
				}
			},
			classItem: {
				params: {
					token: token,
					userToken: userToken,
					parentId: ''
				}
			}
		},
		ready: function() {
			this.getBuyList(); //初始加载
		},
		methods: {
			getBuyList: function() {
				this.$http.get(this.apiUrl, this.item)
				.then(function(response) {
					var result = JSON.parse(response.body);
					if(result.code == 0) {
						var buyData = result.data.list;
						this.buyList = this.buyList.concat(buyData);
						if(buyData.length == 0 && this.item.params.pageNum == 1) {
							this.noData = true;
						} else {
							this.noData = false;
						}
					}
				});
			},
			getMore: function() {
				var contiueFlag = this.buyList.length < this.item.params.pageNum * 20;
				if(contiueFlag) { // 如果没有更多数据，则不调用远程
					promptBox('没有更多数据了！');
					return;
				}
				this.item.params.pageNum++;
				this.getBuyList();
			},
			getProvince: function(){ //地区筛选
				var provinceList = this.provinceData;
				if(provinceList.length > 0){
					selectProvince(provinceList);
				} else {
					this.$http.get(this.areaUrl, this.areaItem)
					.then(function(response){
						var result = JSON.parse(response.body);
						var provinceData = result.data.list;
						for(var i = 0; i < provinceData.length; i++){
							provinceData[i].name = provinceData[i].areaName;
						}
						this.$set('provinceData', provinceData);
						selectProvince(provinceData);
					});
				}
				//模拟ios下拉 省份select
				function selectProvince(data){
					var showProvinceDom = document.querySelector('#selectProvince');
				    var provinceIdDom = document.querySelector('#provinceId');
				    var provinceId = showProvinceDom.dataset['id'];
			        var provinceName = showProvinceDom.dataset['value'];
			        var provinceSelect = new IosSelect(1,[data],{
		                container: '.selectProvince',
		                cssUnit:'rem',
		                headerHeight: 0.5,
		                itemHeight: 0.48,
		                itemShowCount: 5,
		                oneLevelId: provinceId,
		                callback: function (selectOneObj) {
		                    provinceIdDom.value = selectOneObj.id;
		                    $('#cityName2').text(selectOneObj.name);
		                    showProvinceDom.dataset['id'] = selectOneObj.id;
		                    showProvinceDom.dataset['value'] = selectOneObj.name;
		                    buyList.item.params.areaId = selectOneObj.id;
		                    buyList.getBuyList();
		                }
		        	});
				}
			},
			getClass: function(){ //种类筛选
				if(this.oneClassData.length > 0){
					selectClass(this.oneClassData,this.twoClassData);
				} else {
					var oneClassData = [
					    {'id': '1', 'name': '水果', 'parentId': '0'},
					    {'id': '2', 'name': '蔬菜', 'parentId': '0'},
					    {'id': '3', 'name': '畜牧', 'parentId': '0'},
					    {'id': '4', 'name': '药材', 'parentId': '0'}
					];
					var twoClassData = [
					    {"id": "11", "name": "苹果", "parentId": "1"},
					    {"id": "12", "name": "樱桃", "parentId": "1"},
					    {"id": "13", "name": "柿子", "parentId": "1"},
					    {"id": "21", "name": "白菜", "parentId": "2"},
					    {"id": "22", "name": "菠菜", "parentId": "2"},
					    {"id": "23", "name": "山药", "parentId": "2"},
					    {"id": "31", "name": "羊", "parentId": "3"},
					    {"id": "32", "name": "牛", "parentId": "3"},
					    {"id": "33", "name": "鸡", "parentId": "3"}
					];
					selectClass(oneClassData,twoClassData);
				}
				//模拟ios下拉 商品分类select
				function selectClass(oneData,twoData){
				    var showClassDom = $('#className');
				    var oneClassId = $('#oneClassId');
				    var twoClassId = $('#twoClassId');
			        var oneLevelId = showClassDom.attr('data-oneClass-id');
			        var twoLevelId = showClassDom.attr('data-twoClass-id');
			        var classSelect = new IosSelect(2,[oneData, twoData],{
		            	container: '.selectClass',
		                cssUnit:'rem',
		                headerHeight: 0.5,
		                itemHeight: 0.48,
		                itemShowCount: 5,
		                relation: [1, 0, 0, 0],
		                oneLevelId: oneLevelId,
		                twoLevelId: twoLevelId,
		                callback: function (selectOneObj, selectTwoObj) {
		                	if(selectTwoObj.atindex == 3){
		                		oneClassId.val(selectOneObj.id); 
		                    	oneClassId.attr('data-oneClass-name', selectOneObj.name);
		                    	showClassDom.attr('data-oneClass-id', selectOneObj.id);
		                    	showClassDom.html(selectOneObj.name);
		                    	buyList.item.params.categoryId = selectOneObj.id;
		                    	buyList.getBuyList();
		                	}else{
		                		oneClassId.val(selectOneObj.id); 
			                    oneClassId.attr('data-oneClass-name', selectOneObj.name);
			                    twoClassId.val(selectTwoObj.id);
			                    twoClassId.attr('data-twoClass-name', selectTwoObj.name);
			                    showClassDom.attr('data-oneClass-id', selectOneObj.id);
			                    showClassDom.attr('data-twoClass-id', selectTwoObj.id);
			                    showClassDom.html(selectTwoObj.name);
			                    buyList.item.params.categoryId = selectTwoObj.id;
		                    	buyList.getBuyList();
		                	}
		                }
		        	});
			    }
			}
		},
		directives: { // 自定义v-scroll指令
			scroll: {
				bind: function(el, binding) {
					window.addEventListener('scroll', function() {
						if($(window).scrollTop() + $(window).height() == $(document).height()) {
							buyList.getMore();
						}
					})
				}
			}
		}
	});
});